import { requestClient } from '#/api/request'

import { downloadFileFromBlobPart } from '@vben/utils'

/**
 * 获取工序采购详情
 */
export async function getProcessPurchaseInfo(id: string) {
  return requestClient.get('/admin/ProcessPurchase/edit', { id })
}

/**
 * 获取工序采购列表
 */
export async function getProcessPurchaseList(params: any) {
  return requestClient.get('/admin/ProcessPurchase/index', params)
}

/**
 * 新增工序采购
 */
export function addProcessPurchase(data: any) {
  return requestClient.post('/admin/ProcessPurchase/add', data)
}

/**
 * 编辑工序采购
 */
export function editProcessPurchase(data: any) {
  return requestClient.post('/admin/ProcessPurchase/edit', data)
}

/**
 * 删除工序采购
 */
export function deleteProcessPurchase(ids: string[]) {
  return requestClient.delete('/admin/ProcessPurchase/del', { ids })
}

/**
 * 审核工序采购
 */
export function approvalProcessPurchase(ids: string[]) {
  return requestClient.post('/admin/ProcessPurchase/approval', { ids })
}

/**
 * 反审核工序采购
 */
export function againstProcessPurchase(ids: string[]) {
  return requestClient.post('/admin/ProcessPurchase/against', { ids })
}

/**
 * 财务标记
 */
export function financialSign(ids: string[], type: 'success' | 'error') {
  return requestClient.post('/admin/ProcessPurchase/sign', { ids, type })
}

/**
 * 导出合同
 */
export  function exportContract(contract_type: number, ids: string[]) {
  return requestClient.post('/admin/ProcessPurchase/exportContract', {
    ids,
    contract_type
  })

}

/**
 * 获取附件列表
 */
export function getFileList(params: any) {
  return requestClient.get('/admin/ProcessPurchase/fileList', params)
}

/**
 * 上传附件
 */
export function uploadFile(data: FormData) {
  return requestClient.post('/admin/ProcessPurchase/uploadFile', data, {
    headers: { 'Content-Type': 'multipart/form-data' }
  })
}

/**
 * 删除附件
 */
export function deleteFile(fileId: string) {
  return requestClient.delete('/admin/ProcessPurchase/deleteFile', { fileId })
}

/**
 * 批量导出列表数据
 */
export async function exportListData(search: any) {
  const res = await requestClient.post('/admin/ProcessPurchase/exportList', {
    search
  }, {
    responseType: 'blob',
    responseReturn: 'raw',
  })

  const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
  downloadFileFromBlobPart({
    source: blob,
    fileName: '工序采购列表.xlsx',
  })
}

/**
 * 导出明细数据
 */
export async function exportDetailData(ids: string[]) {
  const res = await requestClient.post('/admin/ProcessPurchase/exportDetail', {
    ids
  }, {
    responseType: 'blob',
    responseReturn: 'raw',
  })

  const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
  downloadFileFromBlobPart({
    source: blob,
    fileName: '工序采购明细.xlsx',
  })
}

/**
 * 获取工序列表
 */
export function getProcessList(params: any) {
  return requestClient.get('/admin/Process/index', params)
}

/**
 * 获取商品列表
 */
export function getGoodsList(params: any) {
  return requestClient.get('/admin/Goods/index', params)
}

/**
 * 获取简码列表
 */
export function getTemplateList(params: any) {
  return requestClient.get('/admin/Template/index', params)
}

/**
 * 获取供应商列表
 */
export function getSupplyList(params: any) {
  return requestClient.get('/admin/Supply/index', params)
}
